package com.yunquan.ohana.base.binding.command

open class BindingCommand<T:Any> {
    private var execute: BindingAction? = null
    private var consume: BindingConsumer<T>? = null
    private var canExecute: BindingFunction<Boolean>? = null

    constructor(execute: BindingAction) {
        this.execute = execute
    }

    constructor(consume: BindingConsumer<T>) {
        this.consume = consume
    }

    constructor(execute: BindingAction, canExecute: BindingFunction<Boolean>) {
        this.execute = execute
        this.canExecute = canExecute
    }

    constructor(consume: BindingConsumer<T>, canExecute: BindingFunction<Boolean>) {
        this.consume = consume
        this.canExecute = canExecute
    }

    constructor()

    fun execute() {
        if (execute != null && canExecute()) {
            execute!!.call()
        }
    }

    fun execute(parameter: T) {
        if (consume != null && canExecute()) {
            consume!!.call(parameter)
        }
    }

    private fun canExecute(): Boolean {
        return canExecute?.call() ?: true
    }
}